#include "../import/head.h"
#include "../wnd_login/head.h"
#include "../wnd_main/head.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "msg_filter.h"

int wnd_loop( int argc, char *argv[] )
{
	using namespace xos_sgsj;
	int ret = 0;

	QApplication a( argc, argv );
	QTextCodec * textCodec = QTextCodec::codecForName( "System" );
	QTextCodec::setCodecForLocale( textCodec );
	QTranslator translator;
	msg_filter filter;

	if( 0 == ret )
	{
		a.installEventFilter( &filter );
		g_pSysData->m_pApp = &a;
	}

	if( 0 == ret )
	{
		char file[1024] = { 0 };
		g_pSysData->m_os.m_pCrt->strcpy( file, g_pSysData->m_os.m_szExePath );
		g_pSysData->m_os.m_pMisc->path_append( file, "res/learn_zh_CN.qm" );
		bool bRet = translator.load( file );
		if( bRet )
		{
			a.installTranslator( &translator );
		}
		else
		{
			ret = 1;
		}
	}

	if( 0 == ret )
	{
		xos_sgsj::data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
		p->init();
		p->m_nMsg = MSG_START_PROGRAM;
		g_pSysData->notify( p );
		p = 0;
	}

	if( 0 == ret )
	{
		login_dlg dlg;
		g_pSysData->m_pLoginDlg = &dlg;
		if( dlg.exec() != QDialog::Accepted )
		{
			ret = 1;
		}
		g_pSysData->m_pLoginDlg = 0;
	}

	if( 0 == ret )
	{
		main_wnd wnd;
		g_pSysData->m_pImpl = &wnd;
		wnd.show();
		ret = a.exec();
		g_pSysData->m_pImpl = 0;
	}

	a.removeEventFilter( &filter );
	g_pSysData->m_pApp = 0;

	return ret;
}

int main( int argc, char *argv[] )
{
    using namespace xos_sgsj;
    int ret = 0;

    if( 0 == ret )
    {
        g_pSysData = new sys_data;
        if( !g_pSysData )
        {
            ret = 1;
        }
    }

    if( 0 == ret )
    {
        ret = g_pSysData->init();
    }

    if( 0 == ret )
	{
		ret = wnd_loop( argc, argv );
    }

    if( g_pSysData )
    {
        g_pSysData->term();
        delete g_pSysData;
        g_pSysData = 0;
    }

    return ret;
}

